<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>

</body>
<script>
    //滑动窗口 计算平均值 窗口大小为k
    const nums = [1, 12, -5, -6, 50, 3];
    function fn(nums, k) {
        let windowsSum = 0;
        for (let i = 0; i < k; i++) {
            windowsSum += nums[i]
        }
        let maxAverage = windowsSum / k
        for (let i = k; i < nums.length; i++) {
            windowsSum = windowsSum - nums[i - k] + nums[i]
            let curMaxAverage = windowsSum / k
            maxAverage = Math.max(curMaxAverage, maxAverage)

        }
        return maxAverage
    }
    console.log(fn(nums, 4));

</script>

</html>